import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class LoadingButton extends StatefulWidget {
  final Widget child;
  final Function()? onPressed;

  const LoadingButton({Key? key, required this.child, required this.onPressed})
      : super(key: key);

  @override
  State<LoadingButton> createState() => _LoadingButtonState();
}

class _LoadingButtonState extends State<LoadingButton> {
  bool executing = false;

  @override
  Widget build(BuildContext context) {
    return ElevatedButton(
        onPressed: widget.onPressed == null || executing
            ? null
            : () async {
                setState(() {
                  executing = true;
                });
                if (widget.onPressed != null) {
                  await widget.onPressed!();
                }
                setState(() {
                  executing = false;
                });
              },
        child: executing
            ? SizedBox(
                height: 15,
                width: 15,
                child: CircularProgressIndicator(),
              )
            : widget.child);
  }
}
